package com.projects.modular.system.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.util.ObjectUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.regex.Pattern;

@Slf4j
public class DateUtil extends DateUtils {
    /**
     * 日期格式
     */
    public static final String DATE_FORMATTER = "yyyy-MM-dd";
    /**
     * 日期格式无分割
     */
    public static final String DATE_FORMATTER_NO_SEGMENTATION = "yyyyMMdd";

    public static final String DATETIME_FORMATTER_NO_SEGMENTATION = "yyyyMMddHHmmss";
    /**
     * 时间格式
     */
    public static final String DATETIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期小时
     */
    public static final String DATE_HOUR_FORMATTER = "yyyy-MM-dd HH:mm";

    /**
     * 小时分钟
     */
    public static final String DATE_HOUR_STR_FORMATTER = "HH点mm分";
    /**
     * 小时分钟
     */
    public static final String HOUR_MINUTE_FORMATTER = "HH:mm";

    public static final String DATE_FORMATTER_NO_HMS = "yyyyMMdd";
    /**
     * 日期月份
     */
    public static final String DATE_MONTH_FORMATER = "yyyy-MM";
    /**
     * 年
     */
    public static final String YEAR_FORMATTER = "yyyy";
    /**
     * 月
     */
    public static final String MONTH_FORMATTER = "MM";
    /**
     * 中国标准时间
     * edg: Thu Feb 02 00:00:00 WET 2012.
     */
    public static final String DATE_EEE_FORMATER = "EEE MMM dd HH:mm:ss zzz yyyy";

    /**
     * 日期带开始时间'T'格式
     */
    public static final String DATE_T_FORMATER = "yyyy-MM-dd'T'HH:mm:ss";

    /**
     * 转成中文日期
     */
    public static final String DATE_STR_FORMATER = "yyyy年MM月dd日";

    /**
     * 日期时间字符串格式
     */
    public static final String DATETIME_STR_FORMATTER = "yyyy年MM月dd日 HH时mm分";

    /**
     * 日期字符串格式
     */
    public static final String DATE_STR_FORMATTER = "yyyy年MM月dd日";

    /**
     * 月日字符串格式
     */
    public static final String MONTH_DATE_FORMATTER = "MM月dd日";

    /**
     * 月日字符串格式
     */
    public static final String MONTH_DATE_SHORT_FORMATTER = "M月d日";

    /**
     * 月日时间字符串格式
     */
    public static final String MONTH_DATE_TIME_FORMATTER = "M月d日HH:mm";

    /**
     * 年/月/日 字符串格式
     */
    public static final String YEAR_MONTH_DATE_FORMATTER = "yyyy/MM/dd";
    /**
     * 今天的最后时间
     */
    public static final String NOW_END_TIME = "yyyy-MM-dd 23:59:59";
    /**
     * 今天的开始时间
     */
    public static final String NOW_START_TIME = "yyyy-MM-dd 00:00:00";

    /**
     * 一天的毫秒数
     */
    public static final Long DAY_MILLISECONDS = 86400000L;



    /**
     * 时间进制
     */
    public static final Integer DAY_RADIX = 24;
    public static final Integer TIME_RADIX = 60;
    public static final Integer MILLISECONDS_RADIX = 1000;

    public static final Long TWO_HOURS = 7200000L;
    public static final Long ONE_DAY = 86400000L;

    public static final  long nd = 1000 * 24 * 60 * 60;
    public static final  long nh = 1000 * 60 * 60;
    public static final  long nm = 1000 * 60;


    /**
     * 获取两个日期相差的月数
     *
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return 如果d2>d1返回 月数差 否则返回0
     */
    public static int getMonthDiff(Date startDate, Date endDate) {
        if (endDate == null || startDate == null) {
            return 0;
        }
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(endDate);
        c2.setTime(startDate);
        if (c1.getTimeInMillis() < c2.getTimeInMillis()) {
            return 0;
        }
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值 假设 d2 = 2015-8-16  d1 = 2011-9-30
        int yearInterval = year1 - year2;
        // 如果 d2的 月-日 小于 d1的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if (month1 < month2 || month1 == month2 && day1 < day2) {
            yearInterval--;
        }
        // 获取月数差值
        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2) {
            monthInterval--;
        }
        monthInterval %= 12;
        return yearInterval * 12 + monthInterval;
    }


    /**
     * 比较两个时间相差的天数
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Long getDaysDiff(LocalDate beginDate, LocalDate endDate) {
        return endDate.toEpochDay() - beginDate.toEpochDay();
    }

    /**
     * 比较两个时间相差的天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Long getDaysDiff(Date startDate, Date endDate) {
        if(ObjectUtils.isEmpty(startDate) || ObjectUtils.isEmpty(endDate)){
            return null;
        }
        LocalDate beginTime = DateUtil.getDateToLocalDate(startDate);
        LocalDate endTime = DateUtil.getDateToLocalDate(endDate);
        return getDaysDiff(beginTime, endTime);
    }

    /**
     * 计算两个时间相差的秒数
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static long getSecondDiff(String startTime, String endTime){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long eTime = 0;
        long sTime = 0;
        try {
            eTime = df.parse(endTime).getTime();
            sTime = df.parse(startTime).getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        long diff = (eTime - sTime) / 1000;
        return diff;
    }

    /**
     * 获取相差月份时的时间
     *
     * @param date  日期
     * @param month 距date 相差月份 正数加 负数减
     * @return
     */
    public static String getMonthByDate(Date date, int month, String formatter) {
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATETIME_FORMATTER;
        }
        return dateToString(getDifferInByDate(date, month), formatter);
    }

    /**
     * 获取相差月份时的时间
     *
     * @param date  日期
     * @param month 距date 相差月份 正数加 负数减
     * @return
     */
    public static Date getDifferInByDate(Date date, int month) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, month);
        Date m = c.getTime();
        return m;
    }


    /**
     * 获取localDate日期的开始时间 {@link java.time.LocalTime}
     * eg. 2018-06-11 13:31:31 return 2018-06-11 00:00:00
     *
     * @param localDate
     * @return
     * @author wangfuguo
     */
    public static Date getDateBeginTime(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取localDate日期的结束时间 {@link java.time.LocalTime}
     * eg. 2018-06-11 13:31:31 return 2018-06-11 23:59:59.999999999
     *
     * @param localDate
     * @return
     * @author wangfuguo
     */
    public static Date getDateEndTime(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MAX);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取localDate日期的结束时间 {@link LocalTime}
     * eg. 2018-06-11 13:31:31 return 2018-06-11 23:59:59.000
     *
     * @param localDate
     * @return
     * @author leigang
     */
    public static Date getEndTimeOfDay(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(23, 59, 59, 0));
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * 获取dateTime 的开始时间
     * @return
     */
    public static Date getStartTimeDay(Date dateTime){
        try {
            String format = DateFormatUtils.format(dateTime, NOW_START_TIME);
            Date endTime = DateUtils.parseDate(format, DATETIME_FORMATTER);
            return endTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取dateTime 的结束时间
     * @return
     */
    public static Date getEndTimeDay(Date dateTime){
        try {
            String format = DateFormatUtils.format(dateTime, NOW_END_TIME);
            Date endTime = DateUtils.parseDate(format, DATETIME_FORMATTER);
            return endTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取localDate日期的中午12.00{{@link java.time.LocalTime}
     * eg 2019-03-28 14:32.36 retrun 2019-03-28 12:00:00
     *
     * @param localDate
     * @return
     * @author wugucheng
     */
    public static Date getDateNoon(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.NOON);
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    /**
     * date 转 LocalDate
     *
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    /**
     * 获取localDate日期所在月的开始日期
     * eg. 2018-06-11 13:31:31 return 2018-06-01 00:00:00
     *
     * @param localDate
     * @return
     * @author wangfuguo
     */
    public static Date getMonthFirstDay(LocalDate localDate) {
        LocalDate firstDate = localDate.with(TemporalAdjusters.firstDayOfMonth());
        return getDateBeginTime(firstDate);
    }

    /**
     * 获取localDate日期所在月的结束日期
     * eg. 2018-06-11 13:31:31 return 2018-06-30 23:59:59.999999999
     *
     * @param localDate
     * @return
     * @author wangfuguo
     */
    public static Date getMonthLastDay(LocalDate localDate) {
        LocalDate firstDate = localDate.with(TemporalAdjusters.lastDayOfMonth());
        return getDateEndTime(firstDate);
    }

    /**
     * 获取localDate日期所在年的开始日期
     * eg. 2018-06-11 13:31:31 return 2018-06-01 00:00:00
     *
     * @param localDate
     * @return
     * @author leiGang
     */
    public static Date getYearFirstDay(LocalDate localDate) {
        LocalDate firstDate = localDate.with(TemporalAdjusters.firstDayOfYear());
        return getDateBeginTime(firstDate);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @param formatter
     * @return
     */
    public static String dateToString(Date date, String formatter) {
        if (date == null) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATETIME_FORMATTER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
        return simpleDateFormat.format(date);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @param formatter
     * @return
     */
    public static String dateToStringStr(Date date, String formatter) {
        if (date == null) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATE_STR_FORMATER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
        return simpleDateFormat.format(date);
    }

    /**
     * 毫秒值转日期
     *
     * @param time
     * @param formatter
     * @return
     */
    public static Date timeMillisToDate(long time, String formatter) {
        if (time == 0) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATETIME_FORMATTER;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
            Date date = new Date();
            date.setTime(time);
            return date;
        } catch (Exception e) {
            log.error("毫秒值转日期错误!", e);
            return null;
        }

    }

    /**
     * 字符串转日期
     *
     * @param dateString
     * @param formatter
     * @return
     */
    public static Date stringToDate(String dateString, String formatter) {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATETIME_FORMATTER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            log.error("字符串转日期错误!", e);
            return null;
        }
    }

    /**
     * CST字符串转日期
     *
     * @param dateString
     * @param formatter
     * @return
     */
    public static Date stringCSTToDate(String dateString, String formatter) {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        if (StringUtils.isEmpty(formatter)) {
            formatter = DATE_EEE_FORMATER;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter, Locale.US);
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            log.error("字符串转日期错误!", e);
            return null;
        }
    }

    /**
     * java.util.Date --> java.time.LocalDate
     */
    public static LocalDate getDateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalDate();
    }

    /**
     * 比较
     *
     * @param startTime endTime
     * @return
     * @Author wugucheng
     */
    public static Long compare(Date startTime, Date endTime) {
        return endTime.getTime() - startTime.getTime();
    }

    /**
     * 得到两日期之间的毫秒数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long subDate(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0;
        }
        return date1.getTime() - date2.getTime();
    }

    /**
     * 计算已用耗时小时
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Long getTwoDateHour(Date date1, Date date2) {
        return getTwoDateTotalMin(date1, date2) / TIME_RADIX;
    }

    /**
     * 计算已用耗时分钟
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Long getTwoDateMin(Date date1, Date date2) {
        return getTwoDateTotalMin(date1, date2) % TIME_RADIX;
    }

    /**
     * 获取两个时间之间的分钟数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long getTwoDateTotalMin(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0L;
        }
        long timeMils = date1.getTime() - date2.getTime();
        return timeMils / (MILLISECONDS_RADIX * TIME_RADIX);
    }

    /**
     * 根据分钟获取小时和分钟
     *
     * @param min
     * @return
     */
    public static String getHourAndMin(Long min) {
        if (min == null || min <= 0) {
            return "0小时0分";
        }
        return min / TIME_RADIX + "小时" + min % TIME_RADIX + "分钟";
    }

    /**
     * 根据毫秒计算天数(向上取整)
     *
     * @param milliseconds
     * @return
     */
    public static Long getDaysByMillisecondsCeil(Long milliseconds) {
        if (milliseconds == null) {
            return null;
        }
        Long radix = (long) DAY_RADIX * (long) TIME_RADIX * (long) TIME_RADIX * (long) MILLISECONDS_RADIX;
        if (milliseconds % radix != 0) {
            return milliseconds / radix + 1;
        }
        return milliseconds / radix;
    }

    /**
     * 判断是否是同一年
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameYear(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameYear = cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
        return isSameYear;
    }

    /**
     * 判断是否是同一月
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameMonth(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameMonth = cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        return isSameMonth;
    }

    /**
     * 判断是否是同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameDay = cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
        return isSameDay;
    }

    /**
     * 获取当前时间+1天（第二天凌晨）
     *
     * @param str num 天数
     * @return
     * @Author wugucheng
     */
    public static Date addMidDay(Date str, int num) {
        Calendar rightNow = Calendar.getInstance();
        Date formatStr = new Date();
        try {
            rightNow.setTime(str);
            rightNow.add(Calendar.DATE, num);
            formatStr = rightNow.getTime();
        } catch (Exception e) {
            log.info("s!", e);
        }
        return formatStr;
    }

    /**
     * 获取当前时间加小时
     *
     * @param str num 小时
     * @return
     * @Author wugucheng
     */
    public static Date addHour(Date str, int num) {
        Calendar rightNow = Calendar.getInstance();
        Date formatStr = new Date();
        try {
            rightNow.setTime(str);
            rightNow.add(Calendar.HOUR, num);
            formatStr = rightNow.getTime();
        } catch (Exception e) {
            log.info("s!", e);
        }
        return formatStr;
    }

    /**
     * 获取时间文本
     *
     * @param hour
     * @param minute
     * @return
     */
    public static String getTimeString(Integer hour, Integer minute) {
        if (hour == null && minute == null) {
            return null;
        }
        if (hour == null) {
            return minute + "分钟";
        }
        if (minute == null) {
            return hour + "小时";
        }
        return hour + "小时" + minute + "分钟";
    }

    /**
     * 获取下 num 月时间
     *
     * @param date
     * @param num
     * @return
     */
    public static Date addMidMonth(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        Date formatStr = new Date();
        try {
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, num);
            formatStr = calendar.getTime();
        } catch (Exception e) {
            log.info("s!", e);
        }
        return formatStr;
    }

    /**
     * 日期转星期几
     *
     * @param date
     * @return -1表示无效
     */
    public static int dateToWeek(Date date) {
        if (date == null) {
            return -1;
        }
        int[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0) {
            return -1;
        }
        return weekDays[w];
    }

    /**
     * 返回date的时分
     *
     * @param date
     * @return
     */
    public static String getDateHourAndMinus(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        return simpleDateFormat.format(date);
    }

    /**
     * 返回date的年月日
     *
     * @param date
     * @return
     */
    public static String getDateYearAndMonthAndDay(Date date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }

    /**
     * 获取单独的日并将date->int
     *
     * @param date
     * @return
     */
    public static int getDayCount(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date.getTime());
        int day = cal.get(Calendar.DATE);
        return day;
    }

    /**
     * 获取当月总天数
     *
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当年总天数
     *
     * @param date
     * @return
     */
    public static int getDaysOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
    }

    /**
     * 获取当前时间-n月
     *
     * @param str num 月数
     * @return
     */
    public static Date reduceMonth(Date str, int num) {
        Calendar rightNow = Calendar.getInstance();
        Date formatStr = new Date();
        try {
            rightNow.setTime(str);
            rightNow.add(Calendar.MONTH, -num);
            formatStr = rightNow.getTime();
        } catch (Exception e) {
            log.info("s!", e);
        }
        return formatStr;
    }

    /**
     * 获取两个日期之间的所有日期 (不包含结束日期)
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<Date> getBetweenDates(Date startDate, Date endDate) {
        List<Date> result = new ArrayList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startDate);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endDate);
        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    /**
     * 获取两个日期之间的日期字符串
     *
     * @param startDate
     * @param endDate
     * @param dateFormatStr
     * @return
     */
    public static List<String> getBetweenDatesStr(String startDate, String endDate, String dateFormatStr) {
        List<String> days = new ArrayList<>();
        DateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
        try {
            Date start = dateFormat.parse(startDate);
            Date end = dateFormat.parse(endDate);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * yyyy-mm格式获取相差num月格式
     *
     * @param month
     * @param num
     * @return
     */
    public static String getMonthAddNum(String month, int num) {
        Date date = DateUtil.stringToDate(month, DateUtil.DATE_MONTH_FORMATER);
        return DateUtil.dateToString(DateUtil.addMidMonth(date, num), DateUtil.DATE_MONTH_FORMATER);
    }

    /**
     * 获取当前时间当年最后一天
     *
     * @param date
     * @return Date
     */
    public static Date getLastDayOfYear(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
        cal.set(Calendar.DAY_OF_YEAR, last);
        return cal.getTime();

    }

    /**
     * 获取传入时间是哪月
     *
     * @param date 日期
     * @return
     */
    public static int getDateOfMonth(Date date) {
        if (date == null) {
            return 0;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        return c1.get(Calendar.MONTH) + 1;
    }

    /**
     * 事件+分钟数
     *
     * @param date 日期
     * @return
     */
    public static Date addMinuteToDate(Date date, int num) {
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_HOUR_FORMATTER);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.add(Calendar.MINUTE, num);
        return stringToDate(sdf.format(c1.getTime()), DateUtil.DATE_HOUR_FORMATTER);
    }

    /**
     * 返回指定月份的最后一天的59分59秒
     */
    public static Date getLastMonthOfDate(Date date) {
        Calendar para = Calendar.getInstance(java.util.Locale.CHINA);
        para.setTime(date);
        para.set(Calendar.DATE, para.getActualMaximum(Calendar.DAY_OF_MONTH));
        para.set(Calendar.HOUR_OF_DAY, 23);
        para.set(Calendar.MINUTE, 59);
        para.set(Calendar.SECOND, 59);
        return para.getTime();
    }

    /**
     * 获取本周的开始时间
     *
     * @return
     */
    public static Date getBeginTimeOfWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    /**
     * 获取上月第一天
     */
    public static Date getLastMonthFirstDay() {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        return DateUtil.stringToDate(myFormatter.format(cal.getTime()) + " 00:00:00", DateUtil.DATETIME_FORMATTER);
    }

    /**
     * 获取上月最后一天
     */
    public static Date getLastMonthEndDay() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 是分割日期格式
     *
     * @param date
     * @return
     */
    public static boolean isDivDate(String date) {
        if (StringUtils.isBlank(date)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$");
        boolean matches = pattern.matcher(date).matches();
        if (!matches) {
            return false;
        }
        return true;
    }

    /**
     * 是无分割日期格式
     *
     * @param date
     * @return
     */
    public static boolean isNoDivDate(String date) {
        if (StringUtils.isBlank(date)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^([0-9]\\d{3}((0[1-9]|1[012])(0[1-9]|1\\d|2[0-8])|(0[13456789]|1[012])(29|30)|(0[13578]|1[02])31)|(([2-9]\\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00))0229)$");
        boolean matches = pattern.matcher(date).matches();
        if (!matches) {
            return false;
        }
        return true;
    }

    /**
     * 是日期格式字符串
     *
     * @param date
     * @return
     */
    public static boolean isDateStr(String date) {
        if (StringUtils.isBlank(date)) {
            return false;
        }
        return isDivDate(date) || isNoDivDate(date);
    }

    /**
     * 两个时间相差是否大于两小时
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean subTimeThanTwoHours(Date startDate, Date endDate) {
        return endDate.getTime() - startDate.getTime() >= TWO_HOURS;
    }

    /**
     * 两个时间相差是否大于一天
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean subTimeThanOneDay(Date startDate, Date endDate) {
        return endDate.getTime() - startDate.getTime() >= ONE_DAY;
    }

    /**
     * 当前时间是否在两个时间段之内（跨日期，小时）
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isBetweenTime(LocalTime startTime, LocalTime endTime, LocalTime betweenTime) {
        return betweenTime.isAfter(startTime) || betweenTime.isBefore(endTime);
    }

    /**
     * date转 LocalTime
     *
     * @param date
     * @return
     */
    public static LocalTime dateToLocalTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime.toLocalTime();
    }

    /**
     * 转有分割日期
     *
     * @param dateStr
     * @return
     */
    public static String toDivDateStr(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }
        if (isDivDate(dateStr)) {
            return dateStr;
        }
        if (isNoDivDate(dateStr)) {
            return StringUtils.substring(dateStr, 0, 4) + "-" + StringUtils.substring(dateStr, 4, 6) + "-" + StringUtils.substring(dateStr, 6, 8);
        }
        return null;
    }

    /**
     * 获取传入日期所在月的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayDateOfMonth(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        return cal.getTime();
    }

    /**
     * 获取传入日期所在月的最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        return cal.getTime();
    }

    /**
     * 根据时间往前往后推N分钟
     * @param time
     * @param number
     * @return
     */
    public static Date adjustTimeFromNumber(Date time, Integer number){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.MINUTE, number);
        return calendar.getTime();
    }

    /**
     * 获取精确到秒的时间戳
     * @param date
     */
    public static String getSecondTimestamp(Date date) {
        if (null == date) {
            return "0";
        }
        return String.valueOf(date.getTime() / 1000);
    }

    /**
     * 计算两个时间相差的天 时 分
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDiffDayHourMinute(Date endDate, Date nowDate) {
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = Math.abs(diff / nd);
        // 计算差多少小时
        long hour = Math.abs(diff % nd / nh);
        // 计算差多少分钟
        long min = Math.abs(diff % nd % nh / nm);
        return day + "天" + hour + "小时" + min + "分钟";
    }
    /**
     * 计算两个时间相差的天 时 分
     * @param endDate
     * @param nowDate
     * @return 返回Map
     */
    public static Map<String,Long> getDiffDayHourMinuteMap(Date endDate, Date nowDate) {
        Map<String,Long> dhmMap = new HashMap();
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = Math.abs(diff / nd);
        // 计算差多少小时
        long hour = Math.abs(diff % nd / nh);
        // 计算差多少分钟
        long min = Math.abs(diff % nd % nh / nm);
        dhmMap.put("day",day);
        dhmMap.put("hour",hour);
        dhmMap.put("min",min);
        return dhmMap;
    }

    /**
     * 获取今天星期几
     * @param nowDate 今日的日期
     * @return
     */
    public static String getDayOfTheWeek(Date nowDate){
        String[] weeks = {"周日","周一","周二","周三","周四","周五","周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(nowDate);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        return weeks[week_index];
    }

    /**
     * endTime 在当前时间之前
     * @param nowTime
     * @param endTime
     * @return
     */
    public static Boolean isBeforeNowTime(Date nowTime,Date endTime){
        if(ObjectUtils.isEmpty(nowTime) || ObjectUtils.isEmpty(endTime)){
            return null;
        }
        if(nowTime.getTime() > endTime.getTime()){
            return true;
        }else if(nowTime.getTime() < endTime.getTime()){
            return false;
        }
        return null;
    }

    /**
     * long转date
     * @param dateLong 毫秒
     * @return
     */
    public Date longToDate(long dateLong){
        Date date = new Date(dateLong);
        return date;
    }

    /**
     * date转long
     * @param date
     * @return
     */
    public long dateToLong(Date date){
        long dateLong = date.getTime();
        return  dateLong;
    }

    /**
     * 获取当前日期的前一天
     * @return
     */
    public static String getCurrentDateBeforeDay(){
        Calendar cal =Calendar.getInstance();
        cal.add(Calendar.DATE,   -1);
        String yesterday = new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime());
        return yesterday;
    }

    /**
     * 将秒转换为 天，小时，分钟
     * @param second
     * @return
     */
    public static String secondToDayHourMin(Long second){
        long days = second / (60 * 60 * 24);
        long hours = (second % (60 * 60 * 24)) / (60 * 60);
        long minutes = (second % (60 * 60)) / (60);
        long seconds = second % 60;
        if(seconds >= 30){
            minutes++;
        }
        return days + "," + hours + "," + minutes;
    }

    /**
     * 字符串日期转小时
     * @param date 字符串日期
     * @param format 日期格式
     * @return
     */
    public static Integer stringDateToHour(String date,String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
        String hhToString = null;
        try {
            hhToString = sdf.format( simpleDateFormat.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Integer hhToInt = Integer.valueOf(hhToString);
        return hhToInt;
    }

    /**
     * 获取一年的开始时间
     * @param date
     * @return
     */
    public static Date getBeginDayOfYear(String date) {
        LocalDate today;
        if (null==date){
            today = LocalDate.now();
        }else {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            today=LocalDate.parse(date, fmt);
        }
        today = LocalDate.of(today.getYear(), Month.JANUARY, 1);
        LocalDateTime localDateTime = today.atTime(00, 00, 01);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        Date dateTime = Date.from(zonedDateTime.toInstant());
        return dateTime;
    }

    /**
     * 获取一年的结束时间
     * @param date
     * @return
     */
    public static Date getEndDayOfYear(String date) {
        LocalDate today;
        if (null==date){
            today = LocalDate.now();
        }else {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            today=LocalDate.parse(date, fmt);
        }
        today = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
        LocalDateTime localDateTime = today.atTime(23, 59, 59);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        Date dateTime = Date.from(zonedDateTime.toInstant());
        return dateTime;
    }
}

